"use strict";
//import initialState from './reducer'
import {combineReducers} from 'redux';
import _ from 'lodash';
let tempPage = 0;
let pageInitial = {
    current:0,
    pages:[
        {
            name:'用户登录',
            uri:'login'
        },
        {
            name:'用户注册',
            uri:'register'
        },
        {
            name:'我的影像',
            uri:'images'
        },
        {
            name:'影像详情',
            uri:'image-detail'
        },
        {
            name:'查看影像',
            uri:'image-view'
        },
        {
            name:'添加影像',
            uri:'image-add'},
        {
            name:'亲友管理',
            uri:'patients'
        },
        {
            name:'添加亲友信息',
            uri:'patient-add'
        },
        {
            name:'编辑亲友信息',
            uri:'patient-edit'
        },
        {
            name:'用户信息',
            uri:'user'
        },
        {
            name:'修改密码',
            uri:'reset-pw'
        },
        {
            name:'设置头像',
            uri:'avatar'
        },
        {
            name:'审核影像',
            uri:'_admin'
        },
        {
            name:'影像详情',
            uri:'examine-detail'
        },
        {
            name:'角色管理',
            uri:'role-list'
        },
        {
            name:'编辑角色信息',
            uri:'role-edit'
        },
        {
            name:'新增角色',
            uri:'role-add'
        },
        {
            name:'用户管理',
            uri:'user-list'
        },
        {
            name:'编辑用户信息',
            uri:'user-edit'
        },
        {
            name:'新增用户',
            uri:'user-add'
        },
        {
            name:'管理员列表',
            uri:'admin-list'
        },
        {
            name:'编辑管理员',
            uri:'admin-edit'
        },
        {
            name:'新增管理员',
            uri:'admin-add'
        },
        {
            name:'专家管理',
            uri:'specialist-list'
        },
        {
            name:'待审核专家',
            uri:'specialist-listunpass'
        },
        {
            name:'专家编辑',
            uri:'specialist-edit'
        },
        {
            name:'新增专家',
            uri:'specialist-add'
        },
        {
            name:'专家详情',
            uri:'specialist-detail'
        },
        {
            name:'科室数据',
            uri:'data-dictionary-list/department'
        },
        {
            name:'设备类型数据',
            uri:'data-dictionary-list/modality'
        },
        {
            name:'职称数据',
            uri:'data-dictionary-list/title'
        },
        {
            name:'系统设置',
            uri:'system-setting'
        },
        {
            name:'日志管理',
            uri:'logger-manage'
        },
        {
            name:'咨询管理',
            uri:'consult-manage'
        }, {
            name:'打印刻盘',
            uri:'order-manage'
        }, {
            name:'报告模板管理',
            uri:'template-manage'
        }, {
            name:'App管理',
            uri: 'app-manage'
        }, {
            name:'发布新版app',
            uri: 'app-public'
        },{
            name:'消息管理',
            uri:'message-manage'
        },
        {
            name:'收费类型',
            uri: 'chargetype'
        },
        {
            name:'工作台',
            uri: 'work-board'
        },
        {
            name:'预约列表',
            uri: 'subscribe-list'
        },
        {
            name:'接诊记录',
            uri: 'diagnose-records'
        },
        {
            name:'检查分类',
            uri: 'checktype'
        },
        {
            name:'危重值',
            uri: 'criticality'
        },
        {
            name:'性别',
            uri: 'gender'
        },
        {
            name:'设备类型',
            uri: 'modality'
        },
        {
            name:'民族',
            uri: 'nationality'
        },
        {
            name:'患者类型/来源',
            uri: 'patienttype'
        },
        {
            name:'检测结果',
            uri: 'testresult'
        },
        {
            name:'职称',
            uri: 'title'
        },
        {
            name:'身份证件类别',
            uri: 'idtype'
        },
        {
            name:'区域',
            uri: 'region'
        },
        {
            name:'权限管理',
            uri: 'action'
        },
        {
            name:'角色管理',
            uri: 'role'
        },
        {
            name:'科室管理',
            uri: 'department'
        },
        {
            name:'产品类别管理',
            uri: 'producttype'
        },
        {
            name:'产品管理',
            uri: 'product'
        },
        {
            name:'专家产品管理',
            uri: 'specialist-product'
        },
        {
            name:'我的产品',
            uri: 'my-product'
        },
        {
            name:'编辑个人资料',
            uri: 'specUser'
        }
    ]
};
function getUri(cb,sp){
    const uri = window.location.hash.replace(/^\#\//,'').replace(/\?[\w|\W]*$/,'').match(/[\w|\-]*/)[0];
    if(typeof cb == 'function'){
        cb(uri,sp)
    }
    return uri
}
function _pages(state = pageInitial){
    let uri = getUri(),                             //location的uri
      newState = state,
      _uri = newState.pages[newState.current] && newState.pages[newState.current].uri;//state里的当前的uri
    if(_uri != uri){
        const _index = _.indexOf(newState.pages,_.filter(newState.pages,o=>{
            return o.uri == uri
        })[0]);
        pageInitial = Object.assign({},pageInitial,{current:_index});
        newState = pageInitial;
    }
    return newState
}
function images(state = {
    isFetch : false,
    items:[]
},action){
    switch (action.type){
        case 'REQUEST_IMAGES':
            return Object.assign({},state,{isFetch : true});
        case 'RECEIVE_IMAGES':
        case 'CLEAR_IMAGES':
            return Object.assign({},state,{isFetch : false,items:action.images});
        case 'UPDATE_IMAGES':
            const newImageId = action.image.id;
            let newItems = state.items.map(item=>{
                if(item.id == newImageId){
                    return Object.assign({},item,action.image)
                }else{
                    return item
                }
            });
            return Object.assign({},state,{isFetch : false,items:newItems});
        case 'DELETE_IMAGE':
            const studyId = action.id;
            _.remove(state.items,item=>{
                return item.id == studyId
            });
            return Object.assign({},state,{isFetch : false,items:state.items});
        default:
            return state
    }
}
function user(state = {
    accessToken: '',
    user: {}
},action){
    switch (action.type){
        case 'GET_USER':
          let _userData = {};
            if(action.user.accessToken){
                _userData = action.user
            }else{
                _userData = {
                    accessToken: state.accessToken,
                    user: action.user.user
                }
            }
            if(window.sessionStorage){
              window.sessionStorage.setItem('user',JSON.stringify(_userData))
            }
            return Object.assign({},state,_userData);
        case 'UPDATE_USER':
            return Object.assign({},state,action.user);
        case 'CLEAR_USER':
            if(window.sessionStorage){
              window.sessionStorage.removeItem('user')
            }
              return Object.assign({},state,{
                  accessToken: '',
                  user: {}
              });
        default:
            return state
    }
}
function portrait(state={
    url:''
},action){
    switch (action.type){
        case 'FETCH_PORTRAIT':
             return Object.assign({},state,{url:action.url});
        default:
            return state
    }
}
function patients(state = {
    isFetch : false,
    items:[]
},action){
    switch (action.type){
        case 'REQUEST_PATIENTS':
            return Object.assign({},state,{isFetch:true});
        case 'RECEIVE_PATIENTS':
        case 'CLEAR_PATIENTS':
            return Object.assign({},state,{isFetch:false,items:action.patients});
        default:
            return state
    }
}
function specialist(state = {
    isFetch : false,
    items:[]
},action){
    switch (action.type){
        case 'REQUEST_SPECIALIST':
            return Object.assign({},state,{isFetch:true});
        case 'RECEIVE_SPECIALIST':
        case 'CLEAR_SPECIALIST':
        /*
        * 这个地方需要改，接口对接后需要将返回值改为：
        * Object.assign({},state,{items:action.patients})
        * actions文件中fetchPatients的方法需要在catch部分去除分发action的代码
        * */
            return Object.assign({},state,{isFetch:false,items:action.specialist});
        default:
            return state
    }
}
const rootReducer = combineReducers({
    images,
    user,
    specialist,
    patients,
    portrait,
    _pages
});
export default rootReducer